package edu.asu.sefcom.extract.profile;

import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public abstract class Parser {

	Logger logger = Logger.getLogger(Parser.class);

	public abstract void parseData(String fileName);
	
	public String userName;
	
	public void setUserName(String userName){
		this.userName=userName;
	}

	public List<String> removeheadertrailer(List<String> resultList,
			String header, String trailer) {
		List<String> resultL = new ArrayList<String>();
		logger.debug("remove header trailer called");
		for (String result : resultList) {
			int index = header.length();
			int end = trailer.length();
			int end1 = result.length() - end;
			result = result.substring(index, end1 - 1);
			logger.debug(result + "done");
			resultL.add(result);
		}
		return resultL;
	}

	public void readPosts(String fileData, String start, String end,
			int fromInd, List<String> resultList) {
		// System.out.println(fileData);
		boolean return1 = false;
		int beginIndex = 0;
		int endIndex = 0;
		while (beginIndex != -1) {
			beginIndex = fileData.indexOf(start, fromInd);
			endIndex = fileData.indexOf(end, beginIndex);
			int endlength = end.length();
			if (beginIndex != -1) {
				logger.debug("adding**********" + beginIndex + " : " + endIndex);
				String postData = fileData.substring(beginIndex, endIndex
						+ endlength);
				// //pick all the middle parts and place in list
				logger.debug("added" + postData);
				resultList.add(postData);
			}
			fromInd = beginIndex + 1;
		}
		if (resultList.size() == 0) {
			useRegexForPost(fileData, start, end, 0, resultList);
		}
		return;
	}

	public void useRegexForPost(String fileData, String start, String end,
			int fromInd, List<String> resultList) {
		try {
			Pattern p = Pattern.compile(start + ".*" + end, Pattern.DOTALL);
			Matcher m = p.matcher(fileData);
			String res = "";
			while (m.find()) {
				res = m.group();
				resultList.add(res);
			}
		} catch (Exception e) {
		}
	}

	public static String readFile(String fileName)
			throws IllegalArgumentException {
		String docData = "";
		FileInputStream inStream = null;
		try {
			File file = new File(fileName);
			String charsetName = "UTF-8";
			inStream = new FileInputStream(file);
			MappedByteBuffer byteData = inStream.getChannel().map(
					FileChannel.MapMode.READ_ONLY, 0, file.length());
			docData = Charset.forName(charsetName).decode(byteData).toString();
			inStream.close();
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}

		Logger.getLogger(Parser.class).debug("---$$$---" + docData);
		return docData;
	}

	public void checkForEmpty(String postDate, String heading,
			String originalContent, String commentCount, String tags,
			Map<String, Boolean> mapOfEmptyFields) {
		if (mapOfEmptyFields.isEmpty()) {
			// System.out.println("populating");
			mapOfEmptyFields.put("DATE", false);
			mapOfEmptyFields.put("HEADING", false);
			mapOfEmptyFields.put("POST", false);
			mapOfEmptyFields.put("COMMENT", false);
			mapOfEmptyFields.put("TAG", false);
		}
		// System.out.println(postDate.isEmpty());
		boolean res = postDate.isEmpty() ? false : mapOfEmptyFields.put("DATE",
				true);
		res = heading.isEmpty() ? false : mapOfEmptyFields.put("HEADING", true);
		res = originalContent.isEmpty() ? false : mapOfEmptyFields.put("POST",
				true);
		res = commentCount.isEmpty() ? false : mapOfEmptyFields.put("COMMENT",
				true);
		res = tags.isEmpty() ? false : mapOfEmptyFields.put("TAG", true);

	}

}
